#include<stdio.h>
#include<at89c51ed2.h>
#include<mcs51reg.h>
#include<malloc.h>
#include<stdlib.h>
#include "io.h"
#include "lcd.h"



#define HEAP_SIZE 1600	/*heap size of 1600*/
unsigned char xdata heap[HEAP_SIZE];


void lcdData(char val,int reg);
int lcdDataRead();



/*****************************************************************************/
/**Critical Initialisation. Serial Communication at 19200 baud, XRAM 
 * 1024 bytes activated. This function has reference on Page 41 of 
 * the SDCC Manual*/
void _sdcc_external_startup() {
	AUXR|=0x0C;	/*XRAM 1024 bytes activated*/
	TMOD|=0x20;	/*Timer 1 Mode 2*/
	TH1=0xFD;	/*Timer Value 253 for baud rate 9600*/
	SCON = 0x50;/*SCON: mode 1, 8-bit UART, enable receiver*/
	PCON|=0x80;	/*double baud rate to 19200*/
	TR1 = 1; 	/*Start Timer 1 */
	TI = 1;		/*set TI flag for putchar sequence*/
	RI = 0;		/*clear RI flag for getchar sequence*/
	
}
/*****************************************************************************/

int timer=0;










	

void main()
{	


char *  value;
char *  value1;
int x;
int y;
int i;
int num;
char tmp;
char choice;
int initial;
xdata char * str;
char row;
char column;
int test;



/*initialisation functions*/
	init_dynamic_memory((MEMHEADER xdata *)heap, HEAP_SIZE); 


value=(char *)malloc(10);






/*LCD Function test Program */

value1=(char *)malloc(50);
/*initialisation functions*/




/*****************************************************************/
/*lcd init test*/
lcdinit(); //Lcd initilisation
printf("LCD  has Initialised!\n\r");
newLine();


lcddelay(16000);

//while(1)
//lcdgotoxy('0','F');
////lcdgotoaddr(0x01);
//lcdputch('Z');
////test=lcdDataRead();
////test&=0x7F;
////printf("Value is %x",test);

















initial=0;
while(1){
printf("Welcome to LCD Test Program\n\r");
newLine();
printf("Enter which function you wish to test\n\r");
printf("1 : lcdgotoaddr()	2 : lcdgotoxy()\n\r");
printf("3 : lcdputch()		4 : lcdputstr()\n\r");

if(initial==0){
lcdputstr("Welcome to ESD Lab 4, Please Connect via the Terminal for option");
}

choice=getchar();

if(initial==0){
	lcdData(0x01,0);
	lcddelay(500);
}
initial++;


switch (choice){
	case '1': printf("Testing lcdgotoaddr\n\r ");
/*****************************************************************/
/*lcd go to address test*/
printf("Enter the Address you want to seek to, control will print a X at that location\t ");
i=0;
gets(value);

lcdgotoaddr(lcd_asciiInput(value));
lcdputch('X');
free(value);
break;
/*******************************************************************/
   case '2' : printf("Testing lcdgotoxy\n\r"); /*lcdgotoxy   */
   
   
	printf("\n\rEnter Row:\t");
	row=getchar();
	putchar(row);
    printf("\n\rEnter Column:\t");
	column=getchar();
	putchar(column);
   	lcdgotoxy(row,column);
   	lcdputch('X');
   	
  	 break;
   
   case '3': printf("Testing putch\n\r");
   			 newLine();
   			 printf("Enter character you wish, will be printed at current cursor address\n\r");
   			 *value=getchar();
   			 lcdputch(*value);
   			 free(value);
  	 		  break;
  case '4': printf("Enter a String, you should also enter  co-ordinated of where it will be printed\n\r");
  			i=0;
 			
  				str=(xdata char *)malloc(500);
				gets(str);
				
  			
  			
   				lcdputstr(str);
  				
   				free(str);
  				break;
	}	
   
}   
   
 
}
